package com.seal.yoyoshop.web;

import com.seal.yoyoshop.bin.Goods;
import com.seal.yoyoshop.bin.Type;
import com.seal.yoyoshop.service.GoodsService;
import com.seal.yoyoshop.service.TypeService;
import com.seal.yoyoshop.service.impl.GoodsServiceImpl;
import com.seal.yoyoshop.service.impl.TypeServiceImpl;
import com.seal.yoyoshop.util.PageUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "HotServlet", value = "/index/top")
public class HotServlet extends HttpServlet {
    TypeService typeService = new TypeServiceImpl();
    GoodsService goodsService = new GoodsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        获取前端发来的数据
        String strtypeId = request.getParameter("typeid");
        String strpage = request.getParameter("page");
        String strsize = request.getParameter("size");
//        string转int
        int typeId = strtypeId == null ? 2 : Integer.parseInt(strtypeId);
        int page = strpage == null ? 1 : Integer.parseInt(strpage);
        int size = strsize == null ? 16 : Integer.parseInt(strsize);
//        头部导航栏
        request.setAttribute("flag",typeId + 5);
        List<Type> typeList = typeService.getTypeList();
        request.setAttribute("typeList",typeList);
//        根据typeid获取热销/新品
        request.setAttribute("typeid",typeId);
//        查热销
        List<Goods> goodsList = goodsService.getGoodsListByHotType(typeId,page,size);
        request.setAttribute("goodList",goodsList);

//        获取分页信息,添加页码
        long total = goodsService.getCountByHotType(typeId);
        System.out.println(total);
        String pageInfo = PageUtil.getPageTool(request,total,page,size);
        request.setAttribute("pageTool",pageInfo);

        request.getRequestDispatcher("/index/goods.jsp").forward(request,response);
    }
}
